package com.powernode.controller;

import com.powernode.component.LoginUserHolder;
import com.powernode.dto.AppUserDto;
import com.powernode.dto.LoginUser;
import com.powernode.dto.Result;
import com.powernode.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
/**
 * 在controller层直接对方法中的形参进行校验时，需要在类上加上@Validated注解
 */
@Validated
@RestController
@RequestMapping("/ums/app/appUsers")
public class AppUserController {

    @Autowired
    private AppUserService service;
    @Autowired
    private LoginUserHolder holder;

    /**
     * 获取登录用户
     */
    @GetMapping("/loginUser")
    public Result<LoginUser> loginUser(){
        return Result.success("获取登录用户信息成功",holder.getLoginUser());
    }

    /**
     * 获取验证码
     */
    @GetMapping("/loginCode/{mobile}")
    public Result<String> getLoginCode(@PathVariable @NotEmpty(message = "手机号不能为空") String mobile) {
        return service.getLoginCode(mobile);
    }

    /**
     * 根据用户手机号和验证码获取用户对象
     */
    @GetMapping("/byMobileAndCode")
    public Result<AppUserDto> getAppUserByMobileAndCode(
            @NotBlank(message = "手机号不能为空") String mobile,@NotBlank(message = "验证码不能为空") String code) {
        return service.getAppUserByMobileAndCode(mobile, code);
    }

    /**
     * 根据用户名获取用户对象
     */
    @GetMapping("/byUsername")
    public Result<AppUserDto> getAppUserByMobileAndCode( @NotBlank(message = "用户名不能为空") String username) {
        return service.getAppUserByUsername(username);
    }
}
